package com.runlooker.runer.ui.personInfo;

import org.jivesoftware.smack.XMPPException;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.runlooker.runer.R;
import com.runlooker.runer.application.MyApplication;
import com.runlooker.runer.dao.PersonInfoDao;
import com.runlooker.runer.entity.PersonInfoEntity;
import com.runlooker.runer.service.XmppService;
import com.runlooker.runer.ui.base.BaseActivity;
import com.runlooker.runer.util.PreferenceUtils;
import com.runlooker.runer.util.T;

public class ModifyActivity extends BaseActivity {
	private final static String tag = ModifyActivity.class.getSimpleName();
	public final static String TITLE = "title";// 被修改项的名称
	public final static String CONTENT = "content";// 需要被修改的内容
	public final static String ITEM = "item";// 被修改的项
	private final static int SUCCESS = 1;

	public final static int AREA = 2;
	public final static int FIELD = 3;
	public final static int NICK_NAME = 4;
	public final static int ADDRESS = 5;

	public final static int ACTIVITY_RESULT = 1;

	private Button mSubmitBtn;
	private EditText mEdit;
	private TextView mTitleTV;
	private String mTitle;
	private String mContent;
	private int mItem; // 修改的类型
	private boolean isModified = false;
	private PersonInfoDao mPersonInfoDao;
	private PersonInfoEntity mInfo;

	private XmppService mXmppService;

	private final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == SUCCESS) {
				setResult(ACTIVITY_RESULT);
				finish();
			} else {
				T.showLong(ModifyActivity.this, "提交失败！");
			}
		}

	};

	ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mXmppService = ((XmppService.XMPPBinder) service).getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mXmppService = null;
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify);
		initialize();
	}

	private void initialize() {
		mPersonInfoDao = new PersonInfoDao(this);
		Intent intent = new Intent(this, XmppService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

		mSubmitBtn = (Button) findViewById(R.id.submit_personinfo);
		mEdit = (EditText) findViewById(R.id.modify_personinfo);
		mTitleTV = (TextView) findViewById(R.id.modify_title);

		Intent intent2 = getIntent();
		mTitle = intent2.getStringExtra(TITLE);
		mContent = intent2.getStringExtra(CONTENT);
		mItem = intent2.getIntExtra(ITEM, -1);

		mTitleTV.setText(mTitle);
		mEdit.setText(mContent);

		mEdit.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				isModified = true;
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		mSubmitBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isModified) {
					mInfo = mPersonInfoDao.getPersonInfo(PreferenceUtils
							.getPrefString(ModifyActivity.this,
									MyApplication.ACCOUNT_USERNAME_KEY, ""));
					switch (mItem) {
					case AREA:
						mInfo.setCtry(mEdit.getText().toString().trim());
						break;
					case FIELD:
						mInfo.setField(mEdit.getText().toString().trim());
						break;
					case NICK_NAME:
						mInfo.setNickName(mEdit.getText().toString().trim());
						break;
					case ADDRESS:
						mInfo.setAddress(mEdit.getText().toString().trim());
						break;
					default:
						break;
					}

					new Thread(new Runnable() {
						@Override
						public void run() {
							Message msg = Message.obtain();
							try {
								mXmppService.savePersonInfo(mInfo);
								mPersonInfoDao.saveOrUpdatePersonInfo(mInfo);
								msg.what = SUCCESS;
							} catch (XMPPException e) {
								msg.what = -1;
							} finally {
								handler.sendMessage(msg);
							}
						}
					}).start();
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		handler.removeCallbacksAndMessages(null);
		unbindService(mConnection);
	}

	public void back(View view) {
		finish();
	}
}
